package com.gadgets.busi.bm03.process;

import com.gadgets.busi.bm03.common.Bm03Constant;
import com.gadgets.busi.bm03.common.dto.ExcelRowProcessDTO;
import com.gadgets.framework.common.constant.GadgetsExceptionConstant;
import com.gadgets.framework.common.exception.GadgetsValidateException;
import com.gadgets.framework.common.util.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbsSqlGeneratorExcelProcess<T> implements IExcelRowProcess{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    public AbsSqlGeneratorExcelProcess(String moduelType, Integer rowNum) {
        if(StringUtils.isBlank(moduelType) || null==rowNum){
            throw new GadgetsValidateException(GadgetsExceptionConstant.VALIDATE_ERROR,"Excel行处理器的标记参数不允许为空");
        }
        this.moduelType = moduelType;
        this.rowNum = rowNum;
    }

    private final String moduelType;

    private final Integer rowNum;

    @SuppressWarnings("unchecked")
    @Override
    public void excute(Row row, ExcelRowProcessDTO paramContainer) {
        if(judege(paramContainer.getTaskType(), paramContainer.getRowNum())){
            T data = (T) paramContainer.getParamMap().get(Bm03Constant.EXCEL_IMPORT_PROCESS_KEY_PARAM);
            this.realExcute(row, data, paramContainer);
        }
    }

    /**
     * 实际执行方法
     * @param row                   行数据
     * @param data                  拓展数据
     * @param excelRowProcessDTO    入参
     */
    protected abstract void realExcute(Row row, T data, ExcelRowProcessDTO excelRowProcessDTO);

    /**
     * 执行判断器
     * @param taskType  任务类型
     * @param rowNum    行号
     * @return          是否执行
     */
    protected boolean judege(String taskType, int rowNum){
        return moduelType.equals(taskType) && this.rowNum==rowNum;
    }

    protected String getModuelType() {
        return moduelType;
    }

    protected Integer getRowNum() {
        return rowNum;
    }
}
